package com.citytransport.prokopjuk.route;

import java.util.ArrayList;
import java.util.Iterator;
import com.citytransport.prokopjuk.vehicleDTO.*;

/**
 * The Route class define the routes of vehicle.
 * 
 * @author Prokopj'uk Oleksander
 * @verison 1.0
 * */

public class Route {
    private int routeID;
    private ArrayList<Vehicle> vehicleOnRoute;
    private int intervalMinute;
    private ArrayList<Station> stations;
    private int price;

    /** No-arg constructor */
    public Route() {
	vehicleOnRoute = new ArrayList<Vehicle>();
	routeID = 0;
	intervalMinute = 0;
	stations = new ArrayList<Station>();
	price = 0;

    }

    public Route(int routeID, ArrayList<Vehicle> vehicleOnRoute,
	    int intervalMinute, ArrayList<Station> stations, int price) {
	this.routeID = routeID;
	this.setRouteID(routeID);
	this.vehicleOnRoute = new ArrayList<Vehicle>(vehicleOnRoute);
	this.intervalMinute = intervalMinute;
	this.stations = new ArrayList<Station>(stations);
	this.price = price;

    }

    /**
     * Return the number (routeID) of the route item.
     * 
     * @return routeID.
     */
    public int getRouteID() {
	return routeID;
    }

    /**
     * Method setRouteID change number (routeID) of the route with the value
     * that gives.
     * 
     * @param routeID
     *            - new value of routeID.
     */
    public void setRouteID(int routeID) {
	this.routeID = routeID;
	Iterator<Vehicle> itr = this.vehicleOnRoute.iterator();
	while (itr.hasNext()) {
	    itr.next().setRouteID(routeID);
	}

    }

    public int getPrice() {
	return price;
    }

    public void setPrice(int price) {
	this.price = price;
    }

    /**
     * Return interval between two vehicle of one route.
     * 
     * @return intervalMinute.
     * */
    public int getIntervalMinute() {
	return intervalMinute;
    }

    /**
     * Change interval for all vehicle in specified route.
     * 
     * @param intervalMinute
     *            - new interval in minutes.
     */
    public void setIntervalMinute(int intervalMinute) {
	this.intervalMinute = intervalMinute;
    }

    /**
     * Return all vehicle that rides on specified route.
     * 
     * @return list of vehicle in the current route.
     */
    public ArrayList<Vehicle> getVehicleOnRoute() {
	return this.vehicleOnRoute;
    }

    /**
     * Clean up and sign new vehicle on specifies route.
     * 
     * @param vehicles
     *            ArrayList of new vehicles.
     */
    public void setVehicleOnRoute(ArrayList<? extends Vehicle> vehicles) {
	vehicleOnRoute = new ArrayList(vehicles);
//	Vehicle temp;
//	Iterator<Vehicle> itr = vehicleOnRoute.iterator();
//	while (itr.hasNext()) {
//	    temp = itr.next();
//	}
    }

    public ArrayList<Station> getStations() {
	return stations;
    }

    public void setStations(ArrayList<Station> stations) {
	this.stations = new ArrayList<Station>(stations);
    }

    public void addStation(Station station) {
	stations.add(station);
    }

    public void removeStation(Station station) {
	stations.remove(station);
    }

    public void removeStation(int stationID) {
	boolean deleted = false;
	while (!deleted) {
	    Iterator<Station> itr = stations.iterator();
	    if (itr.hasNext()) {
		if (itr.next().getStationID() == stationID) {
		    itr.remove();
		    deleted = true;
		}
	    }
	}
    }

    /**
     * Add new vehicle to this route.
     * 
     * @param newVehicle
     *            vehicle that will add to the route.
     */
    public void addVehicle(Vehicle newVehicle) {
	vehicleOnRoute.add(newVehicle);
    }

    /**
     * Add list of vehicle to the route.
     * 
     * @param vehicleList
     *            list of new vehicle.
     */
    public void addVehicle(ArrayList<Vehicle> vehicleList) {
	Vehicle temp;
	Iterator<Vehicle> newItr = vehicleList.iterator();
	while (newItr.hasNext()) {
	    temp = newItr.next();
	    this.vehicleOnRoute.add(temp);
	}
    }

    /**
     * Removing vehicle from the route.
     * 
     * @param an
     *            object of some Vehicle .
     */
    public void removeVehicle(Vehicle vehicle) {
	vehicleOnRoute.remove(vehicle);
    }

    /**
     * Remove Vehicle from route input the number of vehicle.
     * 
     * @param vehicleNumberToRemove
     *            the number of vehicle that will be removed.
     */
    public void removeVehicle(String vehicleNumberToRemove) {
	String numberToRemove = vehicleNumberToRemove.trim();
	Vehicle tmp;
	Iterator<Vehicle> itr = this.vehicleOnRoute.iterator();
	while (itr.hasNext()) {
	    tmp = itr.next();
	    if (tmp.getRegistrationNumber().equalsIgnoreCase(numberToRemove)) {
		itr.remove();
		return;
	    }
	}
    }

    @Override
    public String toString() {
	StringBuilder buff = new StringBuilder();
	buff.append("Route number " + getRouteID() + ". The interval is "
		+ getIntervalMinute() +" price = " + price+ ". \nVehicle :  \n");
	Iterator<Vehicle> itr = this.getVehicleOnRoute().iterator();
	while (itr.hasNext()) {
	    buff.append(itr.next() + "\n");
	}
	buff.append("stations : "+ stations);
	return buff.toString();
    }

    /**
     * Return sum of seats of all vehicle on current route.
     * */
    public int getGeneralSeats() {
	int res = 0;
	Iterator<Vehicle> itr = this.vehicleOnRoute.iterator();
	while (itr.hasNext()) {
	    res += itr.next().getSeatAmount();
	}
	return res;
    }
}
